// mircoApp/new_coupon/carNumber/carNumber.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: "",
    info: [],
    car_total: 0,
    page: 1,
    total_page: 0,
    isLoadMore: true,
    isflashSaleCoupon: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoadMore){
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async orderGoodsId() {
    let json = {
      address: "",
      goods: `[{"goods_id":${this.options.orderGoodsId},"total":1,"option_id":"0"}]`,
      member_coupon_ids: "[]",
      orders: "[]",
      invoice: {},
      mark: 0,
      dispatch_type_id: 1,
      first_default: "",
      cart_ids: "[]",
      positioning_success: 0,
      province: "",
      city: "",
      car_id: this.data.radio
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.flash-sale.index"),
      data: json
    })
    if (data.result) {
      wx.navigateTo({
        url: '/packageD/buy/orderPay/orderPay?order_id=' + data.data.order_ids,
      })
    } else {
      app.tips(data.msg);
    }
    console.log(data)
  },
  async bindCarNumber() {
    if (app._isTextEmpty(this.data.radio)) return app.tips('请选择车牌号')
    if (this.options.orderGoodsId) {
      this.orderGoodsId();
      return
    }
    let json = {
      id: this.options.id,
      car_id: this.data.radio
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.member-coupon.bind-car-no"),
      data: json
    })
    if (data.result == 1) {
      app.tips(data.msg);
      wx.redirectTo({
        url: '/mircoApp/new_coupon/new_coupon/new_coupon',
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.index.get-car")
    })
    if (data.result) {
      this.setData({
        info: data.data.list.data,
        total_page: data.data.list.last_page,
        isLoadMore: true,
        car_total: data.data.list.total
      })
    } else {
      app.tips(data.msg);
    }
  },
  tapSelect(e) {
    let {
      id
    } = e.currentTarget.dataset;
    this.setData({
      radio: id
    })
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.new-coupon.frontend.controllers.index.get-car"),
        data: {
          page: this.data.page
        }
      })
      if (data.result) {
        this.setData({
          info: this.data.info.concat(data.data.list.data),
          isLoadMore:true
        })
      } else {
        this.setData({
          page: this.data.page - 1,
          isLoadMore: false
        })
        app.tips(data.msg)
      }
    }
  }
})